package com.it.system.eduorderservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.system.eduorderservice.config.TimeoutOrderMQConfig;
import com.it.system.eduorderservice.entity.UserCourseOrder;
import com.it.system.eduorderservice.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 添加订单
     */
    @PostMapping("/makeOrder")
    public ResponseEntity<String> makeOrder(@RequestBody UserCourseOrder request) {
        UserCourseOrder order = new UserCourseOrder();
        order.setActivityId(request.getActivityId());
        order.setUserId(request.getUserId());
        order.setCourseId(request.getCourseId());
        order.setPrice(request.getPrice());
        order.setTradeNo(UUID.randomUUID().toString().replace("-", ""));
        orderService.save(order);
        //发送超时订单消息，将在15秒后确认支付状态
        rabbitTemplate.convertAndSend(TimeoutOrderMQConfig.ORDER_EXCHANGE,
                TimeoutOrderMQConfig.ORDER_QUEUE_KEY, order.getTradeNo(),
                message -> {
                    message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                    //给消息设置超时时间
                    message.getMessageProperties().setExpiration(String.valueOf(15 * 1000));
                    return message;
                });
        return ResponseEntity.ok("下单成功");
    }


    /**
     * 根据订单号修改订单
     */
    @RequestMapping("/updateOrder")
    public ResponseEntity<String> updateOrder(UserCourseOrder order) {
        orderService.updateById(order);
        return ResponseEntity.ok().body("修改成功");
    }

    /**
     * 删除订单
     */
    @RequestMapping("/deleteOrder/{id}")
    public ResponseEntity<String> deleteOrder(@PathVariable Long id) {
        orderService.removeById(id);
        return ResponseEntity.ok().body("删除成功");

    }

    /**
     * 根据交易号查询订单
     */
    @RequestMapping("/getOrderByReadeNo")
    public UserCourseOrder getOrderByReadeNo(String tradeNo) {
        UserCourseOrder order = orderService.getOne(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getTradeNo, tradeNo)
        );
        return ResponseEntity.ok(order).getBody();
    }

    /**
     * 根据用户id查询订单
     */
    @RequestMapping("/getOrderByUserId")
    public UserCourseOrder getOrderByUserId(Long userId) {
        UserCourseOrder order = orderService.getOne(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getUserId, userId)
        );
        return ResponseEntity.ok(order).getBody();
    }

    /**
     * 根据课程id查询订单
     */
    @RequestMapping("/getOrderByCourseId")
    public UserCourseOrder getOrderByCourseId(Long courseId) {
        UserCourseOrder order = orderService.getOne(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getCourseId, courseId)
        );
        return ResponseEntity.ok(order).getBody();
    }


}
